package com.jzr.bedside.base

import android.content.Context
import com.jzr.bedside.view.LoadingDialog
import rx.Subscription
import rx.subscriptions.CompositeSubscription

open class BasePresenter<T : BaseContract.BaseView?> :
    BaseContract.BasePresenter<T> {
    protected var mView: T? = null
    protected var mCompositeSubscription: CompositeSubscription? = null
    private var loadingDialog: LoadingDialog? = null
    private var context: Context? = null

    protected fun unSubscribe() {
        if (mCompositeSubscription != null) {
            mCompositeSubscription!!.unsubscribe()
        }
    }

    protected fun addSubscrebe(subscription: Subscription?) {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = CompositeSubscription()
        }
        mCompositeSubscription!!.add(subscription)
    }

    override fun attachView(view: T) {
        mView = view

    }

    override fun detachView() {
        mView = null
        unSubscribe()
    }

    open fun showLoadingDialog(context: Context?) {
        dismissLoadingDialog()
        val builder:LoadingDialog.Builder =
            LoadingDialog.Builder(context)
                .setMessage("请稍后 正在加载中...")
                .setCancelable(true)
        loadingDialog = builder.create()
        loadingDialog?.show()
    }

    open fun dismissLoadingDialog() {
        if (null != loadingDialog && loadingDialog!!.isShowing) {
            loadingDialog?.dismiss()
            loadingDialog = null
        }
    }


}